package elements 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import gs.TweenLite;
	import util.TextUtils;
	
	/**
	 * 
	 * @author Kasoi
	 */
	public class MovieClipButton extends Sprite 
	{
		
		////////////////////////////////////////////////////////////////////
		// Public properties
		////////////////////////////////////////////////////////////////////
		
		public function get isEnabled() : Boolean 
		{
			return this._isEnabled;
		}
		
		public function set isEnabled(value: Boolean) : void 
		{
			this._isEnabled = value;
			value ? this.enable() : this.disable();
		}
		
		public function get disabed() : Boolean
		{
			return this.mouseChildren;
		}
		
		////////////////////////////////////////////////////////////////////
		// Private properties
		////////////////////////////////////////////////////////////////////
		
		private var field: TextField;
		
		private var button: MovieClip;
		
		private var trigger: Sprite;
		
		private var isPressed: Boolean = false;
		
		private var _isEnabled: Boolean = false;
		
		private var container: Sprite;
		
		////////////////////////////////////////////////////////////////////
		// Public methods
		////////////////////////////////////////////////////////////////////
		
		public function MovieClipButton(button: MovieClip, text: String = null) 
		{
			var format: TextFormat = new TextFormat(
				'Verdana', 12, 0x000000);
			if (text == null) text = '';
			this.field = TextUtils.makeOneLine(text, format);
			this.button = button;
			this.trigger = new Sprite();
			this.container = new Sprite();
			
			this.container.addChild(this.button);
			this.container.addChild(this.field);
			this.container.addChild(this.trigger);
			
			this.addChild(this.container);
			
			if (text == null) this.field.visible = false;
			
			this.trigger.graphics.beginFill(0xff00ff, 0.0);
			this.trigger.graphics.drawRect(0, 0, this.button.width, this.button.height);
			this.trigger.graphics.endFill();
			
			this.field.y = (this.button.height - this.field.height) / 2;
			this.field.x = ((this.button.width) - this.field.width) / 2;
			
			this.button.stop();
			
			this.trigger.buttonMode = true;
			
			this.trigger.addEventListener(MouseEvent.MOUSE_OVER, this.trigger_onMouseOver);
			this.trigger.addEventListener(MouseEvent.MOUSE_OUT, this.trigger_onMouseOut);
			this.trigger.addEventListener(MouseEvent.MOUSE_DOWN, this.trigger_onMouseDown);
		}
		
		public function disable() : void 
		{
			this.mouseChildren = this.mouseEnabled = false;
			TweenLite.to(this.container, 0.5, { alpha: 0.3 } );
		}
		
		public function enable() : void 
		{
			this.mouseChildren = this.mouseEnabled = true;
			TweenLite.to(this.container, 0.5, { alpha: 1 } );
		}
		
		////////////////////////////////////////////////////////////////////
		// Private methods
		////////////////////////////////////////////////////////////////////
		
		////////////////////////////////////////////////////////////////////
		// Listeners
		////////////////////////////////////////////////////////////////////
		
		private function trigger_onMouseOver(e: MouseEvent) : void 
		{
			if (this.isPressed)
			{
				this.button.gotoAndStop(3);
				return;
			}
			this.button.gotoAndStop(2);
		}
		
		private function trigger_onMouseOut(e: MouseEvent) : void 
		{
			if (this.isPressed) return;
			this.button.gotoAndStop(1);
		}
		
		private function trigger_onMouseDown(e: MouseEvent) : void 
		{
			this.button.gotoAndStop(3);
			this.isPressed = true;
			stage.addEventListener(MouseEvent.MOUSE_UP, this.stage_onMouseUp);
		}
		
		private function stage_onMouseUp(e: MouseEvent) : void 
		{
			this.isPressed = false;
			var mx: Number = this.mouseX;
			var my: Number = this.mouseY;
			var r: Rectangle = this.getBounds(this.trigger);
			if (mx > r.x && mx < r.width && my > r.y && my < r.height) this.button.gotoAndStop(2);
			else this.button.gotoAndStop(1);
			stage.removeEventListener(MouseEvent.MOUSE_UP, this.stage_onMouseUp);
		}
		
	}
	
}